package cn.chenlh.fx.component;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.Objects;

/**
 * 可移动的窗口
 */
public abstract class MovableWin extends Application {
    private double xOffSet = 0;
    private double yOffSet = 0;
    protected Stage stage;
    private final Text titleTxt = new Text("可移动的窗口");

    @Override
    public void start(Stage stage) {
        this.stage = stage;
        VBox root = new VBox();
        root.setStyle("""
-fx-background-color: #3c3f41;
""");
        DropShadow dropShadow = new DropShadow();
        dropShadow.setRadius(10);
        dropShadow.setOffsetX(2);
        dropShadow.setOffsetY(-2);
        dropShadow.setSpread(0.5);
        dropShadow.setColor(Color.WHITE);
        root.setEffect(dropShadow);


        titleTxt.setTextAlignment(TextAlignment.CENTER);
        titleTxt.setFill(Color.valueOf("#fff"));
        StackPane titleTxtWrap = new StackPane();
        titleTxtWrap.setStyle("-fx-font-size: 16px");
        titleTxtWrap.setMaxHeight(30);
        titleTxtWrap.setMinHeight(30);
        titleTxtWrap.getChildren().add(titleTxt);

        Text closeBtn = new Text("x");
        closeBtn.setFill(Color.valueOf("#fff"));
        StackPane closeWrap = new StackPane();
        closeWrap.setStyle("-fx-font-size: 21px");
        closeWrap.setMaxHeight(30);
        closeWrap.setMinHeight(30);
        closeWrap.setMaxWidth(50);
        closeWrap.setMinWidth(50);
        closeWrap.getChildren().add(closeBtn);
        closeWrap.setOnMouseEntered(e->{
            closeWrap.setStyle("-fx-background-color: red; -fx-font-size: 21px;-fx-cursor: pointer;");
        });
        closeWrap.setOnMouseExited(e->{
            closeWrap.setStyle("-fx-font-size: 21px");
        });
        closeWrap.setOnMouseClicked(e->{
            stage.close();
        });


        BorderPane titleBox = new BorderPane();
        titleBox.setLeft(titleTxtWrap);
        titleBox.setRight(closeWrap);
        titleBox.setMaxHeight(30);
        titleBox.setMinHeight(30);
        titleBox.setPadding(new Insets(0, 0, 0, 11));

        Pane contentBox = getRoot();
        contentBox.setPadding(new Insets(10, 10, 10, 10));
        contentBox.setMaxHeight(360);
        contentBox.setMinHeight(360);

        root.getChildren().addAll(titleBox, contentBox);

        // 设置拖动标题栏, 移动窗口
        titleBox.setOnMousePressed(event -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });
        titleBox.setOnMouseDragged(event -> {
            stage.setX(event.getScreenX() - xOffSet);
            stage.setY(event.getScreenY() - yOffSet);
        });

        Scene scene = new Scene(root, 900, 400);
        String cssGlobal = Objects.requireNonNull(getClass().getClassLoader().getResource("global.css")).toExternalForm();
        scene.getStylesheets().add(cssGlobal);

        stage.initStyle(StageStyle.TRANSPARENT);
        stage.getIcons().add(new Image("tools.png"));
        stage.setScene(scene);

        customInit(scene);

        stage.show();
    }

    protected abstract Pane getRoot();
    protected abstract void customInit(Scene scene);

    public void setTitle(String title) {
        titleTxt.setText(title);
    }
    public void setIcon(Image img) {
        stage.getIcons().add(img);
    }
    public void setIcon(String imgPath) {
        stage.getIcons().add(new Image(imgPath));
    }

}